procedure doall(ssn_prefix, ssn_value,
                aamin, aamax, aastep, 
                ffmin, ffmax, ffstep)

char    ssn_prefix   {"SN",prompt="Base name for directories "}
int     ssn_value    {100,min=1,max=1000,prompt="SN"}
int     aamin        {1,min=1,max=10,prompt="Minimum a"}
int     aamax        {5,min=1,max=10,prompt="Maximum a"}
int	aastep       {1,min=1,max=10,prompt="Maximim step"}
int     ffmin        {1,min=1,max=10,prompt="Minimum fwhm"}
int     ffmax        {5,min=1,max=10,prompt="Maximum fwhm"}
int	ffstep       {1,min=1,max=10,prompt="Maximim step"}

begin

char sn_prefix       # prefix for directory names
int sn_value
int amin,amax,astep  # spacing variables
int fmin,fmax,fstep  # fwhm variables

int a, f     # spacing and fwhm - loops
char new_dir
int nmask    # reads file database/Nmask to know how many apertures were found
int na, nf   # nmber of points in spacing and fwhm
char dimstr   # to be used by wtext when building the contamination image
char title   # used by contour to create a title

sn_prefix = ssn_prefix
sn_value = ssn_value

amin = aamin
amax = aamax
astep = aastep
fmin = ffmin
fmax = ffmax
fstep = ffstep

# should check, this should be an integer
na = (amax-amin)/astep
nf = (fmax-fmin)/fstep

if ( access("cont.ql") )
   {
   delete("cont.ql",verify=no)
   }
else
   {
   !touch cont.ql
   }

if ( access("cont.lin") )
   {
   delete("cont.lin",verify=no)
   }
else
   {
   !touch cont.lin
   }


for ( a=amin ; a<=amax ; a=a+astep )
   {
   for ( f=fmin ; f<=fmax ; f=f+fstep )
      {
      apresize.llimit = -1 * a / 2.0 
      apresize.ulimit =  a / 2.0 
      new_dir = sn_prefix // str(sn_value) // "-a" // str(a) // "s" // str(f)
      chdir (new_dir)
print (new_dir)
      !cleansimul
      mask
nmask = 10
      scan(nmask,<"database/Nmask")
      if (nmask == 25 )
         { 
         calfib(nome="mask")
         ifu (ql=yes)
         ifu (ql=no)
         wtext (input="G8IV.lin.ldisp.fits",output="G8IV.lin.ldisp.txt")
         wtext (input="G8IV.ql.ldisp.fits",output="G8IV.ql.ldisp.txt")
         !cont < G8IV.lin.ldisp.txt >> ../cont.lin
         !cont < G8IV.ql.ldisp.txt >> ../cont.ql
         }
      else
         {
         !echo "1.0000" >> ../cont.lin
         !echo "1.0000" >> ../cont.ql
         }
      chdir ("..")
      }
   }

print("Done calculating, creating contamination maps")

print ("LA VAI")
dimstr = str(na+1) // "," // str(nf+1)

print("Dimstr = ",dimstr)

if (imaccess("cont.lin"))
   {
   imdelete("cont.lin",verify=no)
   }
rtext(input="cont.lin",output="cont.lin",otype="",header=no,pixels=yes,
      nskip=0,dim=dimstr)

if (imaccess("cont.ql"))
   {
   imdelete("cont.ql",verify=no)
   }
rtext(input="cont.ql",output="cont.ql",otype="",header=no,pixels=yes,
      nskip=0,dim=dimstr)

#title = "Aperture extraction - y spacing - x fibers width in sigma" \
#        // sn_prefix // str(sn_value)
contour(image="cont.lin",
   floor=0.1,ceiling=0.8,zero=0.,
   ncontours=4,interval=0.2,nhi=-1,dashpat=528,device="stdgraph",
   title="A",
   preserve=yes,label=yes,fill=no,xres=64,yres=64,perimeter=yes,
   vx1=0.,vx2=0.,vy1=0.,vy2=0.,subsample=no,append=no,mode="ql")

#title = "Gaussian extraction - y spacing - x fibers width in sigma" \
#        // sn_prefix // str(sn_value)
contour(image="cont.ql",floor=0.1,ceiling=0.8,zero=0.,
   ncontours=4,interval=0.2,nhi=-1,dashpat=528,device="stdgraph",
   title="B",
   preserve=yes,label=yes,fill=no,xres=64,yres=64,perimeter=yes,
   vx1=0.,vx2=0.,vy1=0.,vy2=0.,subsample=no,append=no,mode="ql")

end
